﻿using System.Diagnostics.Contracts;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

namespace OpenLS.Spreadsheet.UI
{
    public partial class BackgroundFormatterResources 
    {
        public BackgroundFormatterResources()
        {
            InitializeComponent();
        }
        public static DataTemplate Template
        {
            get
            {
                Contract.Ensures(Contract.Result<DataTemplate>() != null);
                BackgroundFormatterResources resources = new BackgroundFormatterResources();
                return (DataTemplate) resources["backgroundTemplate"];
            }
        }

     


        void OnSetNoBackground(object sender, RoutedEventArgs args)
        {
            OnSetFill(sender, null);
            args.Handled = true;
        }

        private void OnSetFill(object sender, object value)
        {
            object o = ((FrameworkElement) sender).DataContext;
            PropertyInfo propertyInfo = o.GetType().GetProperty("Background");
            propertyInfo.SetValue(o, value,null);
        }

        void OnBackgroundChanged(object sender, SelectionChangedEventArgs args)
        {
            
            OnSetFill(sender, new PatternFill((SpreadsheetColor)(((ListBox)sender).SelectedItem)));
            args.Handled = true;
        }
    }
}